import {action, observable} from 'mobx';
import {Form, ListViewPage, RouteLayout} from 'weaver-mobile-page';
import {Modal, Toast, Tools} from "weaver-mobile";
import * as API from '../../api/cptApi';

const getLabel = Tools.getLabel;

export default class AcceptanceStore {
    @observable isLoading0 = true;
    @observable isLoading1 = true;
    @observable isLoading2 = true;
    @observable nowTab = 0;//当前tab:0 1 2
    @observable cptid = '';//资产id
    @observable cptname = '';//资产name
    @observable opt_back = false;//归还
    @observable opt_edit = false;//编辑
    @observable opt_mend = false;//送修
    @observable assetInfoForm = new Form.Store();
    @observable flowLogList = new ListViewPage.Store({route: '/assetInfo/flowLog', id: 1});
    @observable changeLogList = new ListViewPage.Store({route: '/assetInfo/changeLog', id: 2});

    @observable layout = new RouteLayout.Store({route: '/assetInfo/infoCard/edit', id: 'layout'});
    @observable assetEditForm = new Form.Store();

    @observable mapLayout = new RouteLayout.Store({route: '/assetInfo/map', id: 'mapLayout'}); 
    @observable address = '';
    @observable position = [];

    @action
    init = (pathName, id) => {
        this.cptid = id;
        switch (pathName) {
            case 'infoCard':
                this.nowTab = 0;
                this.getCapitalForm(id);
                break;
            case 'flowLog':
                this.nowTab = 1;
                this.flowLogList.clearData();
                this.getCapitalFlowList(id);
                break;
            case 'changeLog':
                this.nowTab = 2;
                this.changeLogList.clearData();
                this.getCapitalChangeList(id);
                break;
            default:
                break;
        }
    };
    @action
    changeTab = (num) => {
        this.nowTab = num;
    }

    @action
    getCapitalForm = (capitalid) => {       //获取form
        this.isLoading0 = true;
        const params = {capitalid: capitalid};
        API.getCapitalForm(params).then((res) => {
            this.isLoading0 = false;
            this.opt_edit = JSON.parse(res.opt_edit);//'false'=>false
            this.opt_back = JSON.parse(res.opt_back);
            this.opt_mend = JSON.parse(res.opt_mend);
            this.opt_delete = JSON.parse(res.opt_delete);
            if (res.title) {
                this.cptname = res.title
            }
            this.assetInfoForm = new Form.Store();
            this.assetInfoForm.initForm(res.fieldinfo, '/assetInfo/infoCard');
            this.address = res.address;
            this.position = [Number(res.position[0]),Number(res.position[1])];
        });
    };
    @action
    getParams = (form) => {
        return form.formParams;
    };

    @action
    updateField = (form, key, value) => {
        form.updateField(key, value);
    };
    @action
    updateFields = (form, fields) => {
        form.updateFields(fields)
    };
    @action
    clearForm = (form) => {
        form.resetFields();
    };
    @action
    getCapitalFlowList = (capitalid) => {   //流转日志长列表
        this.isLoading1 = true;
        const params = {capitalid: capitalid};
        API.getCapitalFlowList(params).then(res => {
            this.isLoading1 = false;
            this.flowLogList.getData({dataKey: res.datas})
        })
    };
    @action
    getCapitalChangeList = (capitalid) => {  //变更日志长列表
        this.isLoading2 = true;
        const params = {capitalid: capitalid};
        API.getCapitalChangeList(params).then(res => {
            this.isLoading2 = false;
            this.changeLogList.getData({dataKey: res.datas})
        })
    };

    @action
    getCapitalEditForm = (history, capitalid) => {//编辑
        this.isLoading0 = true;
        const params = {capitalid: capitalid};
        history.push(this.layout.path);
        API.getCapitalEditForm(params).then(res => {
            this.isLoading0 = false;
            this.assetEditForm = new Form.Store();
            this.assetEditForm.initForm(res.fieldinfo, '/assetInfo/infoCard/edit/layout');
        })
    };
    @action
    saveCapitalEditForm = (capitalid, params, history) => {//保存
        this.isLoading0 = true;
        const paramsAll = {capitalid: capitalid, ...params};
        // history.push(this.layout.path)
        API.doCapitalEdit(paramsAll).then(res => {
            this.isLoading0 = false;
            if (res.flag) {
                Toast.success(getLabel(18758, '保存成功'), 2);
                history.replace('/assetInfo/infoCard/' + this.cptid);
            }
        })
    };
    @action
    saveForm = (history) => {
        const fields = this.getParams(this.assetEditForm);
        let re = this.assetEditForm.doValidate();
        if(re.pass){
            this.saveCapitalEditForm(this.cptid, fields, history);
        }
    };

    @action
    doCapitalDel = (history, cptid) => {
        Modal.alert(getLabel(389861, '资产删除'), getLabel(389863, '确定删除该资产吗') + '?', [
            {
                text: getLabel(31129, "取消"), onPress: () => {
                }
            },
            {
                text: getLabel(826, "确定"), onPress: () => {
                    const params = {
                        capitalid: cptid
                    };
                    API.doCapitalDel(params).then(res => {
                        if (res.flag) {
                            Toast.success(getLabel(20461, '删除成功'), 1);
                            history.go(-1)
                        } else {
                            Toast.fail(res.msg, 2);
                        }
                    })
                }
            }
        ])
    }


}
